<template>
  <CustomScrollbar class="playlists-scroll">
    <div class="playlists">
    <div class="header">
      <h1 class="page-title">播放列表</h1>
      <button class="create-btn" @click="showCreateDialog = true">
        <span>+</span> 创建播放列表
      </button>
    </div>
    
    <div v-if="playlists.length === 0" class="empty-state">
      <div class="empty-icon">📋</div>
      <h3>还没有播放列表</h3>
      <p>创建你的第一个播放列表来开始整理音乐</p>
      <button class="create-btn-large" @click="showCreateDialog = true">
        创建播放列表
      </button>
    </div>
    
    <div v-else class="playlists-grid">
      <div 
        v-for="playlist in playlists" 
        :key="playlist.id"
        class="playlist-card"
        @click="openPlaylist(playlist)"
      >
        <div class="playlist-cover">
          <span class="playlist-icon">🎵</span>
        </div>
        <div class="playlist-info">
          <h3 class="playlist-name">{{ playlist.name }}</h3>
          <p class="playlist-count">{{ playlist.songCount }} 首歌曲</p>
        </div>
      </div>
    </div>
    
    <!-- 创建播放列表对话框 -->
    <div v-if="showCreateDialog" class="dialog-overlay" @click="closeDialog">
      <div class="dialog" @click.stop>
        <h3>创建播放列表</h3>
        <input 
          v-model="newPlaylistName" 
          type="text" 
          placeholder="为你的播放列表命名"
          class="name-input"
          @keyup.enter="createPlaylist"
        >
        <div class="dialog-actions">
          <button class="cancel-btn" @click="closeDialog">取消</button>
          <button 
            class="confirm-btn" 
            @click="createPlaylist"
            :disabled="!newPlaylistName.trim()"
          >
            创建
          </button>
        </div>
      </div>
    </div>
    </div>
  </CustomScrollbar>
</template>

<script>
import CustomScrollbar from '@/components/CustomScrollbar.vue'

export default {
  components: {
    CustomScrollbar
  },
  name: 'PlaylistsView',
  data() {
    return {
      playlists: [],
      showCreateDialog: false,
      newPlaylistName: ''
    }
  },
  methods: {
    openPlaylist(playlist) {
      this.$router.push(`/playlist/${playlist.id}`)
    },
    createPlaylist() {
      if (!this.newPlaylistName.trim()) return
      
      const newPlaylist = {
        id: Date.now(),
        name: this.newPlaylistName.trim(),
        songCount: 0,
        createdAt: new Date()
      }
      
      this.playlists.push(newPlaylist)
      this.closeDialog()
    },
    closeDialog() {
      this.showCreateDialog = false
      this.newPlaylistName = ''
    }
  }
}
</script>

<style scoped>
.playlists-scroll {
  height: 100%;
}

.playlists {
  padding-bottom: 24px; /* 确保底部有适当的间距 */
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
}

.page-title {
  font-size: 2rem;
  margin: 0;
  color: white;
}

.create-btn {
  background: #1DB954;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 24px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s;
}

.create-btn:hover {
  background: #1ed760;
  transform: scale(1.05);
}

.empty-state {
  text-align: center;
  padding: 64px 32px;
  color: #b3b3b3;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 24px;
  opacity: 0.5;
}

.empty-state h3 {
  color: white;
  font-size: 2rem;
  margin-bottom: 16px;
}

.create-btn-large {
  background: #1DB954;
  color: white;
  border: none;
  padding: 16px 32px;
  border-radius: 32px;
  font-weight: bold;
  cursor: pointer;
  margin-top: 24px;
  transition: background 0.2s;
  font-size: 16px;
}

.create-btn-large:hover {
  background: #1ed760;
}

.playlists-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 24px;
}

.playlist-card {
  background: #181818;
  padding: 20px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.playlist-card:hover {
  background: #282828;
  transform: translateY(-4px);
}

.playlist-cover {
  width: 160px;
  height: 160px;
  background: linear-gradient(135deg, #1DB954, #1ed760);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  font-size: 3rem;
  color: white;
  box-shadow: 0 4px 20px rgba(29, 185, 84, 0.3);
}

.playlist-info h3 {
  color: white;
  margin-bottom: 8px;
  font-size: 1.1rem;
}

.playlist-info p {
  color: #b3b3b3;
  margin: 0;
  font-size: 0.9rem;
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog {
  background: #282828;
  border-radius: 8px;
  padding: 32px;
  width: 90%;
  max-width: 400px;
}

.dialog h3 {
  color: white;
  margin-bottom: 24px;
  font-size: 1.5rem;
}

.name-input {
  width: 100%;
  padding: 12px 16px;
  background: #3e3e3e;
  border: 1px solid #535353;
  border-radius: 4px;
  color: white;
  font-size: 16px;
  margin-bottom: 24px;
}

.name-input:focus {
  outline: none;
  border-color: #1DB954;
}

.name-input::placeholder {
  color: #b3b3b3;
}

.dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.cancel-btn,
.confirm-btn {
  padding: 12px 24px;
  border-radius: 24px;
  font-weight: bold;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
}

.cancel-btn {
  background: transparent;
  color: #b3b3b3;
  border: 1px solid #535353;
}

.cancel-btn:hover {
  color: white;
  border-color: white;
}

.confirm-btn {
  background: #1DB954;
  color: white;
}

.confirm-btn:hover:not(:disabled) {
  background: #1ed760;
}

.confirm-btn:disabled {
  background: #535353;
  color: #b3b3b3;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .playlists-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 16px;
  }
  
  .playlist-cover {
    width: 120px;
    height: 120px;
  }
  
  .dialog {
    margin: 20px;
    padding: 24px;
  }
}
</style>